﻿using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JewererBasic.BO
{
    public enum KindReciever
    {
        All,
        Bag,
        Staff,
        Order_Tree,
        Order_Component
    }

    public class ComponentHistory
    {
        public string Component_Id { get; set; } // component export
        public int Numb_Export { get; set; } // so phieu export
        public string Creator { get; set; } // nguoi tao phieu export
        public DateTime Time { get; set; } // thoi gian tao
        public KindReciever Kind { get; set; } // loai export cho bag, staff,...
        public string Worker_Id { get; set; } // nguoi nhan export
        public string Worker_Name { get; set; }
        public string Reciever_Id { get; set; } // export cho doi tuong nao? bag_id, tree_id, order_componnet_id
        public string Comment { get; set; } // reason export or something else
        public double Weight { get; set; }

        #region Create_Export_History
        public static List<ComponentHistory> CreateExportHistory(List<COMPONENT_EXPORTBAGDETAIL> lstExportBag)
        {
            List<ComponentHistory> result = new List<ComponentHistory>();
            foreach (var export_bag in lstExportBag)
            {
                ComponentHistory history = new ComponentHistory();
                history.Component_Id = export_bag.IdComponent;
                history.Creator = export_bag.COMPONENT_EXPORTBAG.IdCreater;
                history.Kind = KindReciever.Bag;
                history.Numb_Export = export_bag.IdExport;
                history.Reciever_Id = export_bag.COMPONENT_EXPORTBAG.IdBag;
                history.Worker_Id = export_bag.COMPONENT_EXPORTBAG.EMPLOYEE.Staff_Id;
                history.Worker_Name = export_bag.COMPONENT_EXPORTBAG.EMPLOYEE.Name;
                history.Comment = export_bag.Reason;
                history.Weight = export_bag.Weight.Value;
                result.Add(history);
            }
            return result;
        }

        public static List<ComponentHistory> CreateExportHistory(List<COMPONENT_EXPORTSTAFFDETAIL> lstExportStaff)
        {
            List<ComponentHistory> result = new List<ComponentHistory>();
            foreach (var export_staff in lstExportStaff)
            {
                ComponentHistory history = new ComponentHistory();
                history.Comment = export_staff.Reason;
                history.Component_Id = export_staff.IdComponent;
                history.Creator = export_staff.COMPONENT_EXPORTSTAFF.IdCreater;
                history.Kind = KindReciever.Staff;
                history.Numb_Export = export_staff.IdExport;
                history.Reciever_Id = export_staff.COMPONENT_EXPORTSTAFF.EMPLOYEE.Staff_Id;
                history.Time = export_staff.COMPONENT_EXPORTSTAFF.DayMake;
                history.Weight = export_staff.Weight;
                result.Add(history);
                //history.Worker_Id=export_st
                //history.Worker_Name
            }
            return result;
        }

        public static List<ComponentHistory> CreateExportHistory(List<COMPONENT_ORDER_COM_MOLECULE_EXPORT> lstExportMolecule)
        {
            List<ComponentHistory> result = new List<ComponentHistory>();
            foreach (var export_molecule in lstExportMolecule)
            {
                ComponentHistory history = new ComponentHistory();
                history.Comment = "Mix";
                history.Component_Id = export_molecule.Com_Slaver_Id;
                //history.Creator=export_molecule.COMPONENT_ORDER_DETAIL.
                history.Kind = KindReciever.Order_Component;
                history.Numb_Export = export_molecule.COMPONENT_ORDER_DETAIL.Component_Order_Id;
                history.Reciever_Id = export_molecule.COMPONENT_ORDER_DETAIL.Component_Id;
                history.Time = export_molecule.TimeMake;
                history.Worker_Id = export_molecule.COMPONENT_ORDER_DETAIL.EMPLOYEE.Staff_Id;
                history.Worker_Name = export_molecule.COMPONENT_ORDER_DETAIL.EMPLOYEE.Name;
                history.Weight = export_molecule.Export_Weight;
                result.Add(history);
            }
            return result;
        }

        public static List<ComponentHistory> CreateExportHistory(List<ORDER_TREE_DETAIL> lstOrderTree)
        {
            List<ComponentHistory> result = new List<ComponentHistory>();
            foreach (var tree in lstOrderTree)
            {
                ComponentHistory history = new ComponentHistory();
                //history.Comment=tree.ORDER_TREE.
                history.Component_Id = tree.Component_Id;
                history.Creator = tree.ORDER_TREE.MC;
                history.Kind = KindReciever.Order_Tree;
                history.Numb_Export = tree.Order_Tree_Detail_Id;
                history.Reciever_Id = tree.Order_Tree_Detail_Id.ToString();
                history.Time = tree.ORDER_TREE.DateMake;
                history.Worker_Id = tree.EMPLOYEE.Staff_Id;
                history.Worker_Name = tree.EMPLOYEE.Name;
                history.Weight = tree.ExportWeight;
                result.Add(history);
            }
            return result;
        }
        #endregion

        #region Create_Import_History

        public static List<ComponentHistory> CreateImportHistory(List<COMPONENT_IMPORTDETAIL> lstComponentImport)
        {
            List<ComponentHistory> result = new List<ComponentHistory>();
            foreach (var import in lstComponentImport)
            {
                ComponentHistory history = new ComponentHistory();
                history.Component_Id = import.IdComponent;
                history.Creator = import.COMPONENT_IMPORT.IdCreater;
                history.Kind = KindReciever.Bag;
                history.Numb_Export = import.COMPONENT_IMPORT.IdImport;
                //history.Reciever_Id;
                //history.Worker_Id = import.COMPONENT_EXPORTBAG.EMPLOYEE.Staff_Id;
                //history.Worker_Name = import.COMPONENT_EXPORTBAG.EMPLOYEE.Name;
                history.Comment = import.Reason;
                history.Weight = import.Weight;
                result.Add(history);
            }
            return result;
        }

        public static List<ComponentHistory> CreateImportHistory(List<COMPONENT_ORDER_DETAIL> lstImportOrderComponent)
        {
            List<ComponentHistory> result = new List<ComponentHistory>();
            foreach (var import in lstImportOrderComponent)
            {
                ComponentHistory history = new ComponentHistory();
                //history.Comment=import
                history.Component_Id = import.Component_Id;
                history.Creator = import.COMPONENT_ORDER.MC;
                history.Kind = KindReciever.Order_Component;
                history.Numb_Export = import.Id;
                history.Time = import.COMPONENT_ORDER.DateMake;
                history.Weight = import.Return_Weight;
                result.Add(history);
            }
            return result;
        }

        public static List<ComponentHistory> CreateImportHistory(List<ORDER_TREE_DETAIL> lstImportTree)
        {
            List<ComponentHistory> result = new List<ComponentHistory>();
            foreach (var import in lstImportTree)
            {
                ComponentHistory history = new ComponentHistory();
                history.Component_Id = import.Component_Id;
                history.Creator = import.ORDER_TREE.MC;
                history.Kind = KindReciever.Order_Tree;
                history.Numb_Export = import.Order_Tree_Detail_Id;
                history.Time = import.ORDER_TREE.DateMake;
                history.Weight = import.ReturnWeight;
                result.Add(history);
            }
            return result;
        }

        #endregion
    }

    public class HistoryBO
    {
        public static ComponentHistoryModel GetComponentExport(ComponentHistoryModel model)
        {
            if (model.TypeReport.Equals("Export"))
            {
                if (model.Kind_Reciever.Equals(KindReciever.All.ToString()))
                {
                    model.BagExport = QueryExportBag(model);
                    model.OrderComponentExport = QueryExportOrderComponent(model);
                    model.OrderTreeExport = QueryExportOrderTree(model);
                    model.StaffExport = QueryExportStaff(model);
                }
                else if (model.Kind_Reciever.Equals(KindReciever.Bag.ToString()))
                {
                    model.BagExport = QueryExportBag(model);
                }
                else if (model.Kind_Reciever.Equals(KindReciever.Order_Component.ToString()))
                {
                    model.OrderComponentExport = QueryExportOrderComponent(model);
                }
                else if (model.Kind_Reciever.Equals(KindReciever.Order_Tree.ToString()))
                {
                    model.OrderTreeExport = QueryExportOrderTree(model);
                }
                else if (model.Kind_Reciever.Equals(KindReciever.Staff.ToString()))
                {
                    model.StaffExport = QueryExportStaff(model);
                }
            }
            else
            {
              
            }
            if (model.BagExport != null)
                if (model.BagExport.Count == 0)
                    model.BagExport = null;
            if (model.OrderComponentExport != null)
                if (model.OrderComponentExport.Count == 0)
                    model.OrderComponentExport = null;
            if (model.OrderTreeExport != null)
                if (model.OrderTreeExport.Count == 0)
                    model.OrderTreeExport = null;
            if (model.StaffExport != null)
                if (model.StaffExport.Count == 0)
                    model.StaffExport = null;
            return model;
        }

        #region Helper functions
        private static List<ComponentHistory> QueryExportBag(ComponentHistoryModel model)
        {
            MSQDBEntities db = new MSQDBEntities();
            List<COMPONENT_EXPORTBAGDETAIL> lstBagExport = (from d in db.COMPONENT_EXPORTBAGDETAIL
                                                            where 
                                                            (model.Component_Id == null || model.Component_Id.Equals(d.IdComponent))
                                                            &&(model.TimeTo == null || DateTime.Compare(d.COMPONENT_EXPORTBAG.DayMake, model.TimeTo.Value) <= 0)
                                                            && (model.TimeFrom == null || DateTime.Compare(d.COMPONENT_EXPORTBAG.DayMake, model.TimeFrom.Value) >= 0)
                                                            select d).ToList();
            return ComponentHistory.CreateExportHistory(lstBagExport);
        }

        private static List<ComponentHistory> QueryExportStaff(ComponentHistoryModel model)
        {
            MSQDBEntities db = new MSQDBEntities();
            List<COMPONENT_EXPORTSTAFFDETAIL> lstStaffExport = (from d in db.COMPONENT_EXPORTSTAFFDETAIL
                                                                where
                                                                (model.Component_Id == null || model.Component_Id.Equals(d.IdComponent))
                                                                && (model.TimeTo == null || DateTime.Compare(d.COMPONENT_EXPORTSTAFF.DayMake, model.TimeTo.Value) <= 0)
                                                                && (model.TimeFrom == null || DateTime.Compare(d.COMPONENT_EXPORTSTAFF.DayMake, model.TimeFrom.Value) >= 0)
                                                                select d).ToList();
            return ComponentHistory.CreateExportHistory(lstStaffExport);
        }

        private static List<ComponentHistory> QueryExportOrderTree(ComponentHistoryModel model)
        {
            MSQDBEntities db = new MSQDBEntities();            
            List<ORDER_TREE_DETAIL> lstTreeExport = (from d in db.ORDER_TREE_DETAIL
                                                     where                                                     
                                                     d.Finish!=-1// has exported
                                                     && (model.Component_Id == null || model.Component_Id.Equals(d.Component_Id))
                                                     && (model.TimeTo == null || DateTime.Compare(d.ORDER_TREE.DateMake, model.TimeTo.Value) <= 0)
                                                     && (model.TimeFrom == null || DateTime.Compare(d.ORDER_TREE.DateMake, model.TimeFrom.Value) >= 0)
                                                     select d).ToList();
            return ComponentHistory.CreateExportHistory(lstTreeExport);
        }

        private static List<ComponentHistory> QueryExportOrderComponent(ComponentHistoryModel model)
        {
            MSQDBEntities db = new MSQDBEntities();
            List<COMPONENT_ORDER_COM_MOLECULE_EXPORT> lstMoleculeExport = (from d in db.COMPONENT_ORDER_COM_MOLECULE_EXPORT
                                                                           where
                                                                           d.IsExported==1
                                                                           && (model.Component_Id == null || model.Component_Id.Equals(d.Com_Slaver_Id))
                                                                           && (model.TimeTo == null || DateTime.Compare(d.TimeMake, model.TimeTo.Value) <= 0)
                                                                           && (model.TimeFrom == null || DateTime.Compare(d.TimeMake, model.TimeFrom.Value) >= 0)
                                                                           select d).ToList();
            return ComponentHistory.CreateExportHistory(lstMoleculeExport);
        }
        #endregion
    }
}